package com.ym.jiujiangeyou;

import com.ym.jiujiangeyou.app.AppManager;
import com.ym.jiujiangeyou.app.MyApplication;
import com.ym.jiujiangeyou.ui.DialogView;
import com.ym.jiujiangeyou.util.Constants;
import com.ym.jiujiangeyou.util.HttpUtil;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
 /**   
 * @author 叶铭   
 * @email yeming_1001@163.com
 * @version V1.0   
 */
public class TrafficActivity extends Activity implements OnClickListener{

	private MyApplication application ;
	private TextView title_text ,city_traffic_tv;
	private LinearLayout back_layout ;
	private Dialog dialog ;
	private Toast toast ;
	Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case Constants.HTTP_REQUEST_ERROR :
				dialog.dismiss() ;
				toast = DialogView.popToast(TrafficActivity.this, R.string.http_connect_error) ;
				toast.show() ;
				break;
			case Constants.HTTP_REQUEST_SUCCESS :
				String text = (String) msg.obj ;
				city_traffic_tv.setText(text) ;
				dialog.dismiss() ;
				break;
			default:
				break;
			}
			
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// 添加Activity到堆栈
		AppManager.getAppManager().addActivity(this);
		super.setContentView(R.layout.city_traffic) ;
		
		title_text = (TextView) findViewById(R.id.title_text) ;
		back_layout = (LinearLayout) findViewById(R.id.title_back) ;
		city_traffic_tv = (TextView) findViewById(R.id.city_traffic_tv) ;
		
		title_text.setText(R.string.title_text_traffic) ;
		back_layout.setOnClickListener(this) ;
		dialog = DialogView.popDialog(this,R.string.isloading) ;
		dialog.show() ;
		
		new Thread(new Runnable() {
			Message msg = new Message() ;
			@Override
			public void run() {
				// TODO Auto-generated method stub
				String s = HttpUtil.httpGet(Constants.GET_TRAFFIC_INFO) ;
				if(s.equals(Constants.CONNECTION_TIMEOUT) || s.equals("FAIL")){
					msg.what = Constants.HTTP_REQUEST_ERROR ;
				}else{
					msg.obj = s ;
					msg.what = Constants.HTTP_REQUEST_SUCCESS ;
				}
				handler.sendMessage(msg) ;
			}
		}).start() ;
	}
	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.title_back:
			Intent intent = new Intent(this, MainActivity.class) ;
			startActivity(intent) ;
			this.finish() ;
			break;

		default:
			break;
		}
	}


}
